import {Controller, Post, All, required} from '../decorator/router'
import {login, upload} from '../decorator/auth'
import {
  createDialog, delDialog, exitDialog,
  getDialogInfoByDialogId,
  getDialogs,
  getMyDialogs,
  getUnreadCountByUserId,
  hideDialog, joinDialog, updateDialogName
} from '../service/dialog'
import * as R from 'ramda'
import mongoose from 'mongoose'

const nodeUrl = require('url')
const oss = require('../utils/oss')
const Dialog = mongoose.model('Dialog')
const ObjectId = mongoose.Types.ObjectId

@Controller('/dialog')
class DialogRouter {
  @Post('/list')
  @login
  async getList(ctx, next) {
    ctx.body = await getDialogs(ctx.session.userId)
  }

  @Post('/unread')
  @login
  async getUnread(ctx, next) {
    ctx.body = {
      data: await getUnreadCountByUserId(ctx.session.userId),
      result: 1
    }
  }

  @Post('/create')
  @login
  @required({
    body: ['members', 'name']
  })
  async create(ctx, next) {
    let {members, name} = ctx.request.body
    members = R.dropRepeats(members.split(','))
    await createDialog(ctx.session.userId, members, name)
    ctx.body = {
      result: 1,
      mes: '聊天室创建成功'
    }
  }

  @Post('/myDialogs')
  @login
  async myDialogs(ctx, next) {
    ctx.body = {
      data: await getMyDialogs(ctx.session.userId),
      result: 1
    }
  }

  @Post('/hide')
  @login
  @required({
    body: ['dialogId']
  })
  async hideDialog(ctx, next) {
    await hideDialog(ctx.session.userId, ctx.request.body.dialogId)
    ctx.body = {
      result: 1,
      mes: '删除成功'
    }
  }

  @Post('/info')
  @login
  @required({
    body: ['id']
  })
  async info(ctx, next) {
    const dialog = await getDialogInfoByDialogId(ctx.request.body.id)
    if (dialog) {
      ctx.body = {
        data: dialog,
        result: 1
      }
    } else {
      ctx.body = {
        mes: '会话不存在',
        result: 0
      }
    }
  }

  @Post('/join')
  @login
  @required({
    body: ['id']
  })
  async join(ctx, next) {
    await joinDialog(ctx.session.userId, ctx.request.body.id)
    ctx.body = {
      result: 1,
      mes: '加入成功'
    }
  }

  @Post('/exit')
  @login
  @required({
    body: ['id']
  })
  async exit(ctx, next) {
    await exitDialog(ctx.session.userId, ctx.request.body.id)
    ctx.body = {
      result: 1,
      mes: '退出成功'
    }
  }

  @Post('/del')
  @login
  @required({
    body: ['id']
  })
  async del(ctx, next) {
    await delDialog(ctx.session.userId, ctx.request.body.id)
    ctx.body = {
      result: 1,
      mes: '解散成功'
    }
  }

  @Post('/kickOut')
  @login
  @required({
    body: ['userId', 'dialogId']
  })
  async kickOut(ctx, next) {
    const {userId, dialogId} = ctx.request.body
    const dialog = Dialog.findOne({
      _id: ObjectId(dialogId),
      createdBy: ObjectId(ctx.session.userId)
    })
    if (dialog) {
      await exitDialog(userId.split(','), dialogId)
      ctx.body = {
        result: 1,
        mes: '删除成功'
      }
    } else {
      ctx.body = {
        result: 0,
        mes: '操作失败'
      }
    }
  }

  @Post('/invite')
  @login
  @required({
    body: ['userId', 'dialogId']
  })
  async invite(ctx, next) {
    const {userId, dialogId} = ctx.request.body
    await joinDialog(userId.split(','), dialogId)
    ctx.body = {
      result: 1,
      mes: '邀请成功'
    }
  }

  @Post('/name')
  @login
  @required({
    body: ['name', 'id']
  })
  async updateName(ctx, next) {
    const {name, id} = ctx.request.body
    await updateDialogName(ctx.session.userId, id, name)
    ctx.body = {
      result: 1,
      mes: '修改成功'
    }
  }

  @Post('/icon')
  @login
  @upload
  async updateIcon(ctx, next) {
    const query = ctx.query
    if (!query.id) {
      return ctx.body = {
        result: 0,
        mes: '参数错误'
      }
    }
    const dialog = await Dialog.findOne({
      _id: ObjectId(query.id),
      createdBy: ObjectId(ctx.session.userId)
    })
    if (dialog) {
      let file = await oss.put(`chat-dialog/${query.id}/icon.png`, ctx.request.files.file.path)
      let url = nodeUrl.parse(file.url)
      if (url.search) {
        url = url.href + '&_=' + Date.now()
      } else {
        url = url.href + '?_=' + Date.now()
      }
      dialog.icon = url
      await dialog.save()
      ctx.body = {
        data: url,
        result: 1,
        mes: '更新成功'
      }
    } else {
      ctx.body = {
        result: 0,
        mes: '群组不存在'
      }
    }
  }
}